/**
* 系统管理 订单管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>"订单管理"</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInlinew" class="user-search">
      <el-form-item label="搜索：">
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInlinew.id" placeholder="输入订单ID"></el-input>
      </el-form-item>
      <el-form-item label="">
        <el-input size="small" v-model="formInlinew.userId" placeholder="输入用户ID"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="handleEdit()">添加</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" @selection-change="selectChange" :data="userData" highlight-current-row v-loading="loading"
              border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="50">
      </el-table-column><el-table-column align="center" sortable prop="id" label="订单ID" width="120">
    </el-table-column><el-table-column align="center" sortable prop="userId" label="用户ID" width="120">
    </el-table-column><el-table-column align="center" sortable prop="gymnasiumId" label="体育馆ID" width="120">
    </el-table-column><el-table-column align="center" sortable prop="siteId" label="场地ID" width="120">
    </el-table-column><el-table-column align="center" sortable prop="orderPrice" label="订单价格" width="120">
    </el-table-column><el-table-column align="center" sortable prop="startTime" label="开始时间" width="120">
    </el-table-column><el-table-column align="center" sortable prop="endTime" label="结束时间" width="120">
    </el-table-column><el-table-column align="center" sortable prop="process" label="预约进程，1为未开始，2为正在进行，3为已完成" width="120">
    </el-table-column><el-table-column align="center" sortable prop="status" label="状态，1为正常，2为删除" width="120">
    </el-table-column><el-table-column align="center" sortable prop="dataee" label="日期" width="120">
    </el-table-column>      <el-table-column label="操作" min-width="100" align="center">
      <template slot-scope="scope">
        <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
        <el-button size="mini" type="danger" @click="indentUser(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click='closeDialog("edit")'>
      <el-form label-width="80px" ref="editForm" :model="editForm" :rules="rules">
        <el-form-item label="订单ID" prop="id">
          <el-input size="small" v-model="editForm.id" auto-complete="off" placeholder="请输入订单ID"></el-input>
        </el-form-item><el-form-item label="用户ID" prop="userId">
        <el-input size="small" v-model="editForm.userId" auto-complete="off" placeholder="请输入用户ID"></el-input>
      </el-form-item><el-form-item label="体育馆ID" prop="gymnasiumId">
        <el-input size="small" v-model="editForm.gymnasiumId" auto-complete="off" placeholder="请输入体育馆ID"></el-input>
      </el-form-item><el-form-item label="场地ID" prop="siteId">
        <el-input size="small" v-model="editForm.siteId" auto-complete="off" placeholder="请输入场地ID"></el-input>
      </el-form-item><el-form-item label="订单价格" prop="orderPrice">
        <el-input size="small" v-model="editForm.orderPrice" auto-complete="off" placeholder="请输入订单价格"></el-input>
      </el-form-item><el-form-item label="开始时间" prop="startTime">
        <el-input size="small" v-model="editForm.startTime" auto-complete="off" placeholder="请输入开始时间"></el-input>
      </el-form-item><el-form-item label="结束时间" prop="endTime">
        <el-input size="small" v-model="editForm.endTime" auto-complete="off" placeholder="请输入结束时间"></el-input>
      </el-form-item><el-form-item label="预约进程，1为未开始，2为正在进行，3为已完成" prop="process">
        <el-input size="small" v-model="editForm.process" auto-complete="off" placeholder="请输入预约进程，1为未开始，2为正在进行，3为已完成"></el-input>
      </el-form-item><el-form-item label="状态，1为正常，2为删除" prop="status">
        <el-input size="small" v-model="editForm.status" auto-complete="off" placeholder="请输入状态，1为正常，2为删除"></el-input>
      </el-form-item><el-form-item label="日期" prop="dataee">
        <el-input size="small" v-model="editForm.dataee" auto-complete="off" placeholder="请输入日期"></el-input>
      </el-form-item>      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click='closeDialog("edit")'>取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  // 导入请求方法
  import store from "../../vuex/store";
  import { UTLSTORE } from "../../api/config"
  import {
    indentList,indentSave,indentDelete} from '../../api/userMG'
  import Pagination from '../../components/Pagination';

  export default {
    data() {
      return {
        nshow: true, //switch开启
        fshow: false, //switch关闭
        loading: false, //是显示加载
        title: '添加用户',
        editFormVisible: false, //控制编辑页面显示与隐藏
        dataAccessshow: false, //控制数据权限显示与隐藏
        // 编辑与添加
        editForm: {
          id: '',
          userId: '',
          gymnasiumId: '',
          siteId: '',
          orderPrice: '',
          startTime: '',
          endTime: '',
          process: '',
          status: '',
          dataee: '',
          token: localStorage.getItem('logintoken')
        },
        // 选择数据
        selectdata: [],
        // rules表单验证
        rules: {
          username: [
            { required: true, message: '请输入用户名', trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            {
              pattern: /^1(3\d|47|5((?!4)\d)|7(0|1|[6-8])|8\d)\d{8,8}$/,
              required: true,
              message: '请输入正确的手机号',
              trigger: 'blur'
            }
          ]
        },
        // 删除用户
        seletedata: {
          ids: '',
          token: localStorage.getItem('logintoken')
        },
        // 请求数据参数
        formInlinew: {
          page: 1,
          limit: 10,
          id: '',
          userId: '',
          gymnasiumId: '',
          siteId: '',
          orderPrice: '',
          startTime: '',
          endTime: '',
          process: '',
          status: '',
          dataee: '',
        },
        // 请求数据参数
        formInline: {
          page: 1,
          limit: 10,
          id: '',
          userId: '',
          gymnasiumId: '',
          siteId: '',
          orderPrice: '',
          startTime: '',
          endTime: '',
          process: '',
          status: '',
          dataee: '',
        },
        //用户数据

        // 数据权限
        UserDept: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        },
        // 选中
        checkmenu: [],
        //参数role
        saveroleId: '',
        // 分页参数
        pageparm: {
          currentPage: 1,
          pageSize: 10,
          total: 10
        }
      }
    },
    // 注册组件
    components: {
      Pagination
    },

    /**
     * 数据发生改变
     */
    watch: {},
    computed: {
      imgUrl() {
        return UTLSTORE;
      },
      userData() {
        return store.state.userDatatoo
      },

    },

    /**
     * 创建完毕
     */
    created() {
      this.userdatafetch()
      // this.userData=store.state.userDatatoo;

    },

    /**
     * 里面的方法只有被调用才会执行
     */
    methods: {
      userdatafetch() {
        indentList(JSON.stringify(this.formInline)).then(res => {
          console.log(res.data)
          store.commit("setUserDatatoo", res.data)
        }).catch(err => console.log(err))
      },
      handleAvatarSuccess(response, file, fileList) {
        this.editForm.profile = '/images/' + file.name;
        console.log(row.profile.name)
      },
      beforeAvatarUpload(file) {
        console.log("file=====", file)
        const isJPG = file.type === 'image/jpeg';
        const isPNG = file.type === 'image/png';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG && !isPNG) {
          this.$message.error('只能上传 JPG 或 PNG 格式的图片！');
        } else if (!isLt2M) {
          this.$message.error('上传图片大小不能超过 2MB!');
        }

        return isJPG || isPNG && isLt2M;
      },    // 获取数据方法
      getdata(parameter) {
        //获取用户列表
        indentList(parameter).then(res => {
          console.log(res)
          // console.log(parameter)
          this.loading = false
          if (res.success == false) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.userData = res.data
            // 分页赋值
            this.pageparm.currentPage = this.formInline.page
            this.pageparm.pageSize = this.formInline.limit
            this.pageparm.total = res.count
          }
        })
      },
      // 编辑、添加提交方法
      submitForm() {
        this.$confirm('确定要操作吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            let obj = {
              indent: this.editForm
            }
            // 修改
            indentSave(obj)
              .then(res => {
                this.userdatafetch()

                if (res.success) {
                  this.$message({
                    type: 'success',
                    message: '操作成功!'
                  })
                  this.getdata(this.formInline)
                } else {
                  this.$message({
                    type: 'info',
                    message: res.msg
                  })
                }
              })
              .catch(err => {
                this.loading = false
                this.$message.error('操作失败，请稍后再试！')
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消操作！'
            })
          })
        this.closeDialog("edit")

      },
      // 删除用户
      indentUser(index, row) {
        this.$confirm('确定要删除吗?', '信息', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            let obj = { id: row.id }
            // 删除
            indentDelete(obj)
              .then(res => {
                console.log(res)
                if (res.success) {
                  this.$message({
                    type: 'success',
                    message: '数据已删除!'
                  })
                  this.getdata(this.formInline)
                } else {
                  this.$message({
                    type: 'info',
                    message: res.msg
                  })
                }
              })
              .catch(err => {
                this.loading = false
                this.$message.error('数据删除失败，请稍后再试！')
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除！'
            })
          })
      },
      //显示编辑界面
      handleEdit: function (index, row) {
        this.editFormVisible = true
        if (row != undefined && row != 'undefined') {
          this.title = '修改订单管理'
          this.editForm.id = row.id
          this.editForm.userId = row.userId
          this.editForm.gymnasiumId = row.gymnasiumId
          this.editForm.siteId = row.siteId
          this.editForm.orderPrice = row.orderPrice
          this.editForm.startTime = row.startTime
          this.editForm.endTime = row.endTime
          this.editForm.process = row.process
          this.editForm.status = row.status
          this.editForm.dataee = row.dataee
        } else {
          this.title = '添加用户'
          this.editForm.id = ''
          this.editForm.userId = ''
          this.editForm.gymnasiumId = ''
          this.editForm.siteId = ''
          this.editForm.orderPrice = ''
          this.editForm.startTime = ''
          this.editForm.endTime = ''
          this.editForm.process = ''
          this.editForm.status = ''
          this.editForm.dataee = ''
        }
      },
      // 选中菜单
      changemenu(arr) {
        let change = []
        for (let i = 0; i < arr.length; i++) {
          if (arr[i].checked) {
            change.push(arr[i].id)
          }
        }
        return change
      },
      // 分页插件事件
      callFather(parm) {
        this.formInline.page = parm.currentPage
        this.formInline.limit = parm.pageSize
        this.getdata(this.formInline)
      },
      //搜索事件
      search() {
        console.log(this.formInlinew)
        let arr = []
        store.state.userDatatoo.map((item) => {
          if (item.username.includes(this.formInlinew.username)
            || item.phone.includes(this.formInlinew.phone)) {
            arr.push(item)
          }
        })
        // console.log(arr)
        this.userData = arr
        // this.getdata(this.formInline)
      },
      // 选择复选框事件
      selectChange(val) {
        this.selectdata = val
      },
      // 关闭编辑、增加弹出框
      closeDialog(dialog) {
        if (dialog == 'edit') {
          this.editFormVisible = false
        } else if (dialog == 'perm') {
          this.dataAccessshow = false
        } else if (dialog == 'unit') {
          this.unitAccessshow = false
        }
      },
      checkPhoneNumber() {
        const phoneReg = /^1[3456789]\d{9}$/
        console.log("--------------", this)
        if (!phoneReg.test(this.phoneNumber)) {

        }
      },
    }
  }
</script>

<style scoped>
  .user-search {
    margin-top: 20px;
    text-align: right;
  }

  .userRole {
    width: 100%;
  }
</style>
